package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.RuleType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.MyListUtils;
import com.ruoyi.system.domain.SysControl;
import com.ruoyi.system.mapper.SysControlMapper;
import com.ruoyi.system.service.ISysControlService;
import com.ruoyi.system.vo.SysControlRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Ericcc
 * @version v0.01
 * @date 2021/2/6
 */
@Service
public class SysControlServiceImpl implements ISysControlService
{
    @Autowired
    private SysControlMapper sysControlMapper;

    /**
     * 查询风控
     *
     * @param controlId 风控ID
     * @return 风控
     */
    @Override
    public SysControl selectSysControlById(Long controlId)
    {
        return sysControlMapper.selectSysControlById(controlId);
    }

    /**
     * 查询风控列表
     *
     * @param sysControl 风控
     * @return 风控
     */
    @Override
    public List<SysControl> selectSysControlList(SysControl sysControl)
    {
        return sysControlMapper.selectSysControlList(sysControl);
    }

    /**
     * 通过返回类查询风控列表
     *
     * @param sysControl 风控
     * @return 风控
     */
    @Override
    public List<SysControlRes> selectSysControlResList(SysControl sysControl)
    {
        return sysControlMapper.selectSysControlResList(sysControl);
    }

    /**
     * 新增风控
     *
     * @param sysControl 风控
     * @return 结果
     */
    @Override
    public int insertSysControl(SysControl sysControl)
    {
        return sysControlMapper.insertSysControl(sysControl);
    }

    /**
     * 修改风控
     *
     * @param sysControl 风控
     * @return 结果
     */
    @Override
    public int updateSysControl(SysControl sysControl)
    {
        return sysControlMapper.updateSysControl(sysControl);
    }

    /**
     * 删除风控对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysControlByIds(String ids)
    {
        return sysControlMapper.deleteSysControlByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除风控信息
     *
     * @param controlId 风控ID
     * @return 结果
     */
    @Override
    public int deleteSysControlById(Long controlId)
    {
        return sysControlMapper.deleteSysControlById(controlId);
    }

    /**
     * 查看通道风控
     * @param channelId
     * @return
     */
    @Override
    public SysControlRes selectSysControlByChannel(Long channelId) {
        SysControl search = new SysControl();
        search.setRelationId(channelId);
        search.setControlType(RuleType.CHANNEL.getCode());
        List<SysControlRes> sysControlResList = sysControlMapper.selectSysControlResList(search);
        if (MyListUtils.isNullOrEmpty(sysControlResList)){
            search = new SysControl();
            search.setControlType(RuleType.SYSTEM.getCode());
            sysControlResList = sysControlMapper.selectSysControlResList(search);
        }
        if (MyListUtils.isNullOrEmpty(sysControlResList)){
            throw new BusinessException("系统规则不存在 请设置系统规则");
        }
        SysControlRes sysControlRes = sysControlResList.get(0);
        return sysControlRes;
    }

    /**
     * 查看用户风控
     * @param userId
     * @return
     */
    @Override
    public SysControlRes selectSysControlByUser(Long userId) {
        SysControl search = new SysControl();
        search.setRelationId(userId);
        search.setControlType(RuleType.PERSON.getCode());
        List<SysControlRes> sysControlResList = sysControlMapper.selectSysControlResList(search);
        if (MyListUtils.isNullOrEmpty(sysControlResList)){
            search = new SysControl();
            search.setControlType(RuleType.SYSTEM.getCode());
            sysControlResList = sysControlMapper.selectSysControlResList(search);
        }
        if (MyListUtils.isNullOrEmpty(sysControlResList)){
            throw new BusinessException("系统规则不存在 请设置系统规则");
        }
        SysControlRes sysControlRes = sysControlResList.get(0);
        return sysControlRes;
    }
}
